﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;
using Telerik.Web.UI;

namespace TenSevenNine.Web
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        private bool requiresDataBinding = true;

        protected override void OnInit(EventArgs e)
        {
            
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (AcmsPage != null && AcmsPage.AcmsContext != null && AcmsPage.AcmsContext.CurrentPost != null)
            {
                var pathParts = AcmsPage.AcmsContext.CurrentPost.Path.Split('/');
                if (pathParts.Length == 1)
                    form1.Attributes["class"] = "Home";
                else if (pathParts.Length == 2)
                    form1.Attributes["class"] = pathParts[1]+"";
                else if (pathParts.Length == 3)
                    form1.Attributes["class"] = pathParts[1] + "_1";
                else
                    form1.Attributes["class"] = "xxx";


                //if (pathParts.Length > 1 && pathParts[1] == "Data-Centre-Management")
                //    SectionNav_Control.CssClass="selected";


            }
            
        }

        public override void DataBind()
        {
            requiresDataBinding = false;
            BindMenu();
            base.DataBind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!Page.IsPostBack && requiresDataBinding)
            {
                DataBind();
            }
            base.OnPreRender(e);
        }

        

        //protected TemplatePage AcmsPage
        //{
        //    get
        //    {
        //        return this.Page as TemplatePage;
        //    }
        //}
        protected Acms.AcmsControls.TemplatePage AcmsPage
        {
            get
            {
                var tmp = this.Page as Acms.AcmsControls.TemplatePage;
                return tmp;
            }
        }

        const string NavigationMenuName = "Navigation";

        private Acms.Bll.Menu _NavigationMenu;
        protected Acms.Bll.Menu NavigationMenu
        {
            get
            {
                if (_NavigationMenu == null)
                {
                    _NavigationMenu = Acms.Bll.Menu.GetByNameAndSiteId(NavigationMenuName, AcmsPage.AcmsContext.SiteId);
                    if (_NavigationMenu == null)
                    {
                        throw new NullReferenceException(string.Format("Unable to load menu '{0}'", NavigationMenuName));
                    }
                }
                return _NavigationMenu;
            }
        }

        private void BindMenu()
        {
            if (AcmsPage != null)
            {
                //Get HomePage
                Post home = Post.GetTopLevelBySiteId(AcmsPage.AcmsContext.SiteId, AcmsPage.AcmsContext.SiteVersion);
                if (home != null)
                {
                    radPanel.Items.Add(BindMenuPost(AcmsPage.AcmsContext, home));
                    if (AcmsPage.AcmsContext.CurrentPost != null && home.Id == AcmsPage.AcmsContext.CurrentPost.Id)
                    {
                        radPanel.Items[0].Selected = true;
                    }
                    List<Post> tmp = Post.GetAllByMenuIdAndParentGuid(AcmsPage.AcmsContext.SiteId, NavigationMenu.Id, home.PostGuid, AcmsPage.AcmsContext.SiteVersion);
                    BindPostChildren(radPanel.Items, AcmsPage.AcmsContext, tmp, NavigationMenu);
                }
            }
        }

        private void BindPostChildren(RadPanelItemCollection items, AcmsContext acmsContext, List<Post> children, Acms.Bll.Menu menu)
        {
            foreach (Post child in children)
            {
                RadPanelItem item = BindMenuPost(acmsContext, child);
                items.Add(item);
                bool expanded = false;
                if (acmsContext.CurrentPost != null && acmsContext.CurrentPost.Id == child.Id)
                {
                    //not sure why this is needed as i thought seting the item to selected should have set the class...
                    item.CssClass = string.Concat(item.CssClass, " rpSelected");
                    item.Selected = true;
                    expanded = true;
                }
                else if (acmsContext.CurrentPost != null && acmsContext.CurrentPost.Path.StartsWith(child.Path) && child.Path != "~")
                {
                    expanded = true;
                }

                if (expanded)
                {
                    List<Post> tmp = Post.GetAllByMenuIdAndParentGuid(AcmsPage.AcmsContext.SiteId, menu.Id, child.PostGuid, acmsContext.SiteVersion);
                    if (tmp != null && tmp.Count > 0)
                    {
                        BindPostChildren(item.Items, acmsContext, tmp, menu);
                    }
                    else
                    {
                        item.Selected = true;
                    }
                }
            }
        }

        private RadPanelItem BindMenuPost(AcmsContext acmsContext, Post post)
        {
            RadPanelItem tmp = new RadPanelItem(post.DisplayName, post.Url);
            string[] paths = post.Path.Split('/');
            if (paths.Length == 2)
            {
                tmp.ChildGroupCssClass = tmp.CssClass = paths[1];//string.Concat(paths[1].ToLower(), paths[2]).Replace(" ", "");
            }


            return tmp;
        }


        
    }
}
